Part 1: EDA

bond= read.csv('bond.csv')
bond = bond[bond$observation_date >= "1974-01-01" & bond$observation_date <= "2021-10-01", ]
bond
##     observation_date   BAA BAA10YM   AAA
## 169       1974-01-01  8.48    1.49  7.83
## 170       1974-02-01  8.53    1.57  7.85
## 171       1974-03-01  8.62    1.41  8.01
## 172       1974-04-01  8.87    1.36  8.25
## 173       1974-05-01  9.05    1.47  8.37
## 174       1974-06-01  9.27    1.73  8.47
## 175       1974-07-01  9.48    1.67  8.72
## 176       1974-08-01  9.77    1.73  9.00
## 177       1974-09-01 10.18    2.14  9.24
## 178       1974-10-01 10.48    2.58  9.27
## 179       1974-11-01 10.60    2.92  8.89
## 180       1974-12-01 10.63    3.20  8.89
## 181       1975-01-01 10.81    3.31  8.83
## 182       1975-02-01 10.65    3.26  8.62
## 183       1975-03-01 10.48    2.75  8.67
## 184       1975-04-01 10.58    2.35  8.95
## 185       1975-05-01 10.69    2.63  8.90
## 186       1975-06-01 10.62    2.76  8.77
## 187       1975-07-01 10.55    2.49  8.84
## 188       1975-08-01 10.59    2.19  8.95
## 189       1975-09-01 10.61    2.18  8.95
## 190       1975-10-01 10.62    2.48  8.86
## 191       1975-11-01 10.56    2.51  8.78
## 192       1975-12-01 10.56    2.56  8.79
## 193       1976-01-01 10.41    2.67  8.60
## 194       1976-02-01 10.24    2.45  8.55
## 195       1976-03-01 10.12    2.39  8.52
## 196       1976-04-01  9.94    2.38  8.40
## 197       1976-05-01  9.86    1.96  8.58
## 198       1976-06-01  9.89    2.03  8.62
## 199       1976-07-01  9.82    1.99  8.56
## 200       1976-08-01  9.64    1.87  8.45
## 201       1976-09-01  9.40    1.81  8.38
## 202       1976-10-01  9.29    1.88  8.32
## 203       1976-11-01  9.23    1.94  8.25
## 204       1976-12-01  9.12    2.25  7.98
## 205       1977-01-01  9.08    1.87  7.96
## 206       1977-02-01  9.12    1.73  8.04
## 207       1977-03-01  9.12    1.66  8.10
## 208       1977-04-01  9.07    1.70  8.04
## 209       1977-05-01  9.01    1.55  8.05
## 210       1977-06-01  8.91    1.63  7.95
## 211       1977-07-01  8.87    1.54  7.94
## 212       1977-08-01  8.82    1.42  7.98
## 213       1977-09-01  8.80    1.46  7.92
## 214       1977-10-01  8.89    1.37  8.04
## 215       1977-11-01  8.95    1.37  8.08
## 216       1977-12-01  8.99    1.30  8.19
## 217       1978-01-01  9.17    1.21  8.41
## 218       1978-02-01  9.20    1.17  8.47
## 219       1978-03-01  9.22    1.18  8.47
## 220       1978-04-01  9.32    1.17  8.56
## 221       1978-05-01  9.49    1.14  8.69
## 222       1978-06-01  9.60    1.14  8.76
## 223       1978-07-01  9.60    0.96  8.88
## 224       1978-08-01  9.48    1.07  8.69
## 225       1978-09-01  9.42    1.00  8.69
## 226       1978-10-01  9.59    0.95  8.89
## 227       1978-11-01  9.83    1.02  9.03
## 228       1978-12-01  9.94    0.93  9.16
## 229       1979-01-01 10.13    1.03  9.25
## 230       1979-02-01 10.08    0.98  9.26
## 231       1979-03-01 10.26    1.14  9.37
## 232       1979-04-01 10.33    1.15  9.38
## 233       1979-05-01 10.47    1.22  9.50
## 234       1979-06-01 10.38    1.47  9.29
## 235       1979-07-01 10.29    1.34  9.20
## 236       1979-08-01 10.35    1.32  9.23
## 237       1979-09-01 10.54    1.21  9.44
## 238       1979-10-01 11.40    1.10 10.13
## 239       1979-11-01 11.99    1.34 10.76
## 240       1979-12-01 12.06    1.67 10.74
## 241       1980-01-01 12.42    1.62 11.09
## 242       1980-02-01 13.57    1.16 12.38
## 243       1980-03-01 14.45    1.70 12.96
## 244       1980-04-01 14.19    2.72 12.04
## 245       1980-05-01 13.17    2.99 10.99
## 246       1980-06-01 12.71    2.93 10.58
## 247       1980-07-01 12.65    2.40 11.07
## 248       1980-08-01 13.15    2.05 11.64
## 249       1980-09-01 13.70    2.19 12.02
## 250       1980-10-01 14.23    2.48 12.31
## 251       1980-11-01 14.64    1.96 12.97
## 252       1980-12-01 15.14    2.30 13.21
## 253       1981-01-01 15.03    2.46 12.81
## 254       1981-02-01 15.37    2.18 13.35
## 255       1981-03-01 15.34    2.22 13.33
## 256       1981-04-01 15.56    1.88 13.88
## 257       1981-05-01 15.95    1.85 14.32
## 258       1981-06-01 15.80    2.33 13.75
## 259       1981-07-01 16.17    1.89 14.38
## 260       1981-08-01 16.34    1.40 14.89
## 261       1981-09-01 16.92    1.60 15.49
## 262       1981-10-01 17.11    1.96 15.40
## 263       1981-11-01 16.39    3.00 14.22
## 264       1981-12-01 16.55    2.83 14.23
## 265       1982-01-01 17.10    2.51 15.18
## 266       1982-02-01 17.18    2.75 15.27
## 267       1982-03-01 16.82    2.96 14.58
## 268       1982-04-01 16.78    2.91 14.46
## 269       1982-05-01 16.64    3.02 14.26
## 270       1982-06-01 16.92    2.62 14.81
## 271       1982-07-01 16.80    2.85 14.61
## 272       1982-08-01 16.32    3.26 13.71
## 273       1982-09-01 15.63    3.29 12.94
## 274       1982-10-01 14.73    3.82 12.12
## 275       1982-11-01 14.30    3.75 11.68
## 276       1982-12-01 14.14    3.60 11.83
## 277       1983-01-01 13.94    3.48 11.79
## 278       1983-02-01 13.95    3.23 12.01
## 279       1983-03-01 13.61    3.10 11.73
## 280       1983-04-01 13.29    2.89 11.51
## 281       1983-05-01 13.09    2.71 11.46
## 282       1983-06-01 13.37    2.52 11.74
## 283       1983-07-01 13.39    2.01 12.15
## 284       1983-08-01 13.64    1.79 12.51
## 285       1983-09-01 13.55    1.90 12.37
## 286       1983-10-01 13.46    1.92 12.25
## 287       1983-11-01 13.61    1.92 12.41
## 288       1983-12-01 13.75    1.92 12.57
## 289       1984-01-01 13.65    1.98 12.20
## 290       1984-02-01 13.59    1.75 12.08
## 291       1984-03-01 13.99    1.67 12.57
## 292       1984-04-01 14.31    1.68 12.81
## 293       1984-05-01 14.74    1.33 13.28
## 294       1984-06-01 15.05    1.49 13.55
## 295       1984-07-01 15.15    1.79 13.44
## 296       1984-08-01 14.63    1.91 12.87
## 297       1984-09-01 14.35    1.83 12.66
## 298       1984-10-01 13.94    1.78 12.63
## 299       1984-11-01 13.48    1.91 12.29
## 300       1984-12-01 13.40    1.90 12.13
## 301       1985-01-01 13.26    1.88 12.08
## 302       1985-02-01 13.23    1.72 12.13
## 303       1985-03-01 13.69    1.83 12.56
## 304       1985-04-01 13.51    2.08 12.23
## 305       1985-05-01 13.15    2.30 11.72
## 306       1985-06-01 12.40    2.24 10.94
## 307       1985-07-01 12.43    2.12 10.97
## 308       1985-08-01 12.50    2.17 11.05
## 309       1985-09-01 12.48    2.11 11.07
## 310       1985-10-01 12.36    2.12 11.02
## 311       1985-11-01 11.99    2.21 10.55
## 312       1985-12-01 11.58    2.32 10.16
## 313       1986-01-01 11.44    2.25 10.05
## 314       1986-02-01 11.11    2.41  9.67
## 315       1986-03-01 10.50    2.72  9.00
## 316       1986-04-01 10.19    2.89  8.79
## 317       1986-05-01 10.29    2.58  9.09
## 318       1986-06-01 10.34    2.54  9.13
## 319       1986-07-01 10.16    2.86  8.88
## 320       1986-08-01 10.18    3.01  8.72
## 321       1986-09-01 10.20    2.75  8.89
## 322       1986-10-01 10.24    2.81  8.86
## 323       1986-11-01 10.07    2.82  8.68
## 324       1986-12-01  9.97    2.86  8.49
## 325       1987-01-01  9.72    2.64  8.36
## 326       1987-02-01  9.65    2.40  8.38
## 327       1987-03-01  9.61    2.36  8.36
## 328       1987-04-01 10.04    2.02  8.85
## 329       1987-05-01 10.51    1.90  9.33
## 330       1987-06-01 10.52    2.12  9.32
## 331       1987-07-01 10.61    2.16  9.42
## 332       1987-08-01 10.80    2.04  9.67
## 333       1987-09-01 11.31    1.89 10.18
## 334       1987-10-01 11.62    2.10 10.52
## 335       1987-11-01 11.23    2.37 10.01
## 336       1987-12-01 11.29    2.30 10.11
## 337       1988-01-01 11.07    2.40  9.88
## 338       1988-02-01 10.62    2.41  9.40
## 339       1988-03-01 10.57    2.20  9.39
## 340       1988-04-01 10.90    2.18  9.67
## 341       1988-05-01 11.04    1.95  9.90
## 342       1988-06-01 11.00    2.08  9.86
## 343       1988-07-01 11.11    2.05  9.96
## 344       1988-08-01 11.21    1.95 10.11
## 345       1988-09-01 10.90    1.92  9.82
## 346       1988-10-01 10.41    1.61  9.51
## 347       1988-11-01 10.48    1.52  9.45
## 348       1988-12-01 10.65    1.54  9.57
## 349       1989-01-01 10.65    1.56  9.62
## 350       1989-02-01 10.61    1.44  9.64
## 351       1989-03-01 10.67    1.31  9.80
## 352       1989-04-01 10.61    1.43  9.79
## 353       1989-05-01 10.46    1.60  9.57
## 354       1989-06-01 10.03    1.75  9.10
## 355       1989-07-01  9.87    1.85  8.93
## 356       1989-08-01  9.88    1.77  8.96
## 357       1989-09-01  9.91    1.72  9.01
## 358       1989-10-01  9.81    1.80  8.92
## 359       1989-11-01  9.81    1.94  8.89
## 360       1989-12-01  9.82    1.98  8.86
## 361       1990-01-01  9.94    1.73  8.99
## 362       1990-02-01 10.14    1.67  9.22
## 363       1990-03-01 10.21    1.62  9.37
## 364       1990-04-01 10.30    1.51  9.46
## 365       1990-05-01 10.41    1.65  9.47
## 366       1990-06-01 10.22    1.74  9.26
## 367       1990-07-01 10.20    1.73  9.24
## 368       1990-08-01 10.41    1.66  9.41
## 369       1990-09-01 10.64    1.75  9.56
## 370       1990-10-01 10.74    2.02  9.53
## 371       1990-11-01 10.62    2.23  9.30
## 372       1990-12-01 10.43    2.35  9.05
## 373       1991-01-01 10.45    2.36  9.04
## 374       1991-02-01 10.07    2.22  8.83
## 375       1991-03-01 10.09    1.98  8.93
## 376       1991-04-01  9.94    1.90  8.86
## 377       1991-05-01  9.86    1.79  8.86
## 378       1991-06-01  9.96    1.68  9.01
## 379       1991-07-01  9.89    1.62  9.00
## 380       1991-08-01  9.65    1.75  8.75
## 381       1991-09-01  9.51    1.86  8.61
## 382       1991-10-01  9.49    1.96  8.55
## 383       1991-11-01  9.45    2.03  8.48
## 384       1991-12-01  9.26    2.17  8.31
## 385       1992-01-01  9.13    2.10  8.20
## 386       1992-02-01  9.23    1.89  8.29
## 387       1992-03-01  9.25    1.71  8.35
## 388       1992-04-01  9.21    1.73  8.33
## 389       1992-05-01  9.13    1.74  8.28
## 390       1992-06-01  9.05    1.79  8.22
## 391       1992-07-01  8.84    2.00  8.07
## 392       1992-08-01  8.65    2.06  7.95
## 393       1992-09-01  8.62    2.20  7.92
## 394       1992-10-01  8.84    2.25  7.99
## 395       1992-11-01  8.96    2.09  8.10
## 396       1992-12-01  8.81    2.04  7.98
## 397       1993-01-01  8.67    2.07  7.91
## 398       1993-02-01  8.39    2.13  7.71
## 399       1993-03-01  8.15    2.17  7.58
## 400       1993-04-01  8.14    2.17  7.46
## 401       1993-05-01  8.21    2.17  7.43
## 402       1993-06-01  8.07    2.11  7.33
## 403       1993-07-01  7.93    2.12  7.17
## 404       1993-08-01  7.60    1.92  6.85
## 405       1993-09-01  7.34    1.98  6.66
## 406       1993-10-01  7.31    1.98  6.67
## 407       1993-11-01  7.66    1.94  6.93
## 408       1993-12-01  7.69    1.92  6.93
## 409       1994-01-01  7.65    1.90  6.92
## 410       1994-02-01  7.76    1.79  7.08
## 411       1994-03-01  8.13    1.65  7.48
## 412       1994-04-01  8.52    1.55  7.88
## 413       1994-05-01  8.62    1.44  7.99
## 414       1994-06-01  8.65    1.55  7.97
## 415       1994-07-01  8.80    1.50  8.11
## 416       1994-08-01  8.74    1.50  8.07
## 417       1994-09-01  8.98    1.52  8.34
## 418       1994-10-01  9.20    1.46  8.57
## 419       1994-11-01  9.32    1.36  8.68
## 420       1994-12-01  9.10    1.29  8.46
## 421       1995-01-01  9.08    1.30  8.46
## 422       1995-02-01  8.85    1.38  8.26
## 423       1995-03-01  8.70    1.50  8.12
## 424       1995-04-01  8.60    1.54  8.03
## 425       1995-05-01  8.20    1.57  7.65
## 426       1995-06-01  7.90    1.73  7.30
## 427       1995-07-01  8.04    1.76  7.41
## 428       1995-08-01  8.19    1.70  7.57
## 429       1995-09-01  7.93    1.73  7.32
## 430       1995-10-01  7.75    1.71  7.12
## 431       1995-11-01  7.68    1.75  7.02
## 432       1995-12-01  7.49    1.78  6.82
## 433       1996-01-01  7.47    1.82  6.81
## 434       1996-02-01  7.63    1.82  6.99
## 435       1996-03-01  8.03    1.76  7.35
## 436       1996-04-01  8.19    1.68  7.50
## 437       1996-05-01  8.30    1.56  7.62
## 438       1996-06-01  8.40    1.49  7.71
## 439       1996-07-01  8.35    1.48  7.65
## 440       1996-08-01  8.18    1.54  7.46
## 441       1996-09-01  8.35    1.52  7.66
## 442       1996-10-01  8.07    1.54  7.39
## 443       1996-11-01  7.79    1.59  7.10
## 444       1996-12-01  7.89    1.59  7.20
## 445       1997-01-01  8.09    1.51  7.42
## 446       1997-02-01  7.94    1.52  7.31
## 447       1997-03-01  8.18    1.49  7.55
## 448       1997-04-01  8.34    1.45  7.73
## 449       1997-05-01  8.20    1.49  7.58
## 450       1997-06-01  8.02    1.53  7.41
## 451       1997-07-01  7.75    1.53  7.14
## 452       1997-08-01  7.82    1.52  7.22
## 453       1997-09-01  7.70    1.49  7.15
## 454       1997-10-01  7.57    1.54  7.00
## 455       1997-11-01  7.42    1.54  6.87
## 456       1997-12-01  7.32    1.51  6.76
## 457       1998-01-01  7.19    1.65  6.61
## 458       1998-02-01  7.25    1.68  6.67
## 459       1998-03-01  7.32    1.67  6.72
## 460       1998-04-01  7.33    1.69  6.69
## 461       1998-05-01  7.30    1.65  6.69
## 462       1998-06-01  7.13    1.63  6.53
## 463       1998-07-01  7.15    1.69  6.55
## 464       1998-08-01  7.14    1.80  6.52
## 465       1998-09-01  7.09    2.28  6.40
## 466       1998-10-01  7.18    2.65  6.37
## 467       1998-11-01  7.34    2.51  6.41
## 468       1998-12-01  7.23    2.58  6.22
## 469       1999-01-01  7.29    2.57  6.24
## 470       1999-02-01  7.39    2.39  6.40
## 471       1999-03-01  7.53    2.30  6.62
## 472       1999-04-01  7.48    2.30  6.64
## 473       1999-05-01  7.72    2.18  6.93
## 474       1999-06-01  8.02    2.12  7.23
## 475       1999-07-01  7.95    2.16  7.19
## 476       1999-08-01  8.15    2.21  7.40
## 477       1999-09-01  8.20    2.28  7.39
## 478       1999-10-01  8.38    2.27  7.55
## 479       1999-11-01  8.15    2.12  7.36
## 480       1999-12-01  8.19    1.91  7.55
## 481       2000-01-01  8.33    1.67  7.78
## 482       2000-02-01  8.29    1.77  7.68
## 483       2000-03-01  8.37    2.11  7.68
## 484       2000-04-01  8.40    2.41  7.64
## 485       2000-05-01  8.90    2.46  7.99
## 486       2000-06-01  8.48    2.38  7.67
## 487       2000-07-01  8.35    2.30  7.65
## 488       2000-08-01  8.26    2.43  7.55
## 489       2000-09-01  8.35    2.55  7.62
## 490       2000-10-01  8.34    2.60  7.55
## 491       2000-11-01  8.28    2.56  7.45
## 492       2000-12-01  8.02    2.78  7.21
## 493       2001-01-01  7.93    2.77  7.15
## 494       2001-02-01  7.87    2.77  7.10
## 495       2001-03-01  7.84    2.95  6.98
## 496       2001-04-01  8.07    2.93  7.20
## 497       2001-05-01  8.07    2.68  7.29
## 498       2001-06-01  7.97    2.69  7.18
## 499       2001-07-01  7.97    2.73  7.13
## 500       2001-08-01  7.85    2.88  7.02
## 501       2001-09-01  8.03    3.30  7.17
## 502       2001-10-01  7.91    3.34  7.03
## 503       2001-11-01  7.81    3.16  6.97
## 504       2001-12-01  8.05    2.96  6.77
## 505       2002-01-01  7.87    2.83  6.55
## 506       2002-02-01  7.89    2.98  6.51
## 507       2002-03-01  8.11    2.83  6.81
## 508       2002-04-01  8.03    2.82  6.76
## 509       2002-05-01  8.09    2.93  6.75
## 510       2002-06-01  7.95    3.02  6.63
## 511       2002-07-01  7.90    3.25  6.53
## 512       2002-08-01  7.58    3.32  6.37
## 513       2002-09-01  7.40    3.53  6.15
## 514       2002-10-01  7.73    3.79  6.32
## 515       2002-11-01  7.62    3.57  6.31
## 516       2002-12-01  7.45    3.42  6.21
## 517       2003-01-01  7.35    3.30  6.17
## 518       2003-02-01  7.06    3.16  5.95
## 519       2003-03-01  6.95    3.14  5.89
## 520       2003-04-01  6.85    2.89  5.74
## 521       2003-05-01  6.38    2.81  5.22
## 522       2003-06-01  6.19    2.86  4.97
## 523       2003-07-01  6.62    2.64  5.49
## 524       2003-08-01  7.01    2.56  5.88
## 525       2003-09-01  6.79    2.52  5.72
## 526       2003-10-01  6.73    2.44  5.70
## 527       2003-11-01  6.66    2.36  5.65
## 528       2003-12-01  6.60    2.33  5.62
## 529       2004-01-01  6.44    2.29  5.54
## 530       2004-02-01  6.27    2.19  5.50
## 531       2004-03-01  6.11    2.28  5.33
## 532       2004-04-01  6.46    2.11  5.73
## 533       2004-05-01  6.75    2.03  6.04
## 534       2004-06-01  6.78    2.05  6.01
## 535       2004-07-01  6.62    2.12  5.82
## 536       2004-08-01  6.46    2.18  5.65
## 537       2004-09-01  6.27    2.14  5.46
## 538       2004-10-01  6.21    2.11  5.47
## 539       2004-11-01  6.20    2.01  5.52
## 540       2004-12-01  6.15    1.92  5.47
## 541       2005-01-01  6.02    1.80  5.36
## 542       2005-02-01  5.82    1.65  5.20
## 543       2005-03-01  6.06    1.56  5.40
## 544       2005-04-01  6.05    1.71  5.33
## 545       2005-05-01  6.01    1.87  5.15
## 546       2005-06-01  5.86    1.86  4.96
## 547       2005-07-01  5.95    1.77  5.06
## 548       2005-08-01  5.96    1.70  5.09
## 549       2005-09-01  6.03    1.83  5.13
## 550       2005-10-01  6.30    1.84  5.35
## 551       2005-11-01  6.39    1.85  5.42
## 552       2005-12-01  6.32    1.85  5.37
## 553       2006-01-01  6.24    1.82  5.29
## 554       2006-02-01  6.27    1.70  5.35
## 555       2006-03-01  6.41    1.69  5.53
## 556       2006-04-01  6.68    1.69  5.84
## 557       2006-05-01  6.75    1.64  5.95
## 558       2006-06-01  6.78    1.67  5.89
## 559       2006-07-01  6.76    1.67  5.85
## 560       2006-08-01  6.59    1.71  5.68
## 561       2006-09-01  6.43    1.71  5.51
## 562       2006-10-01  6.42    1.69  5.51
## 563       2006-11-01  6.20    1.60  5.33
## 564       2006-12-01  6.22    1.66  5.32
## 565       2007-01-01  6.34    1.58  5.40
## 566       2007-02-01  6.28    1.56  5.39
## 567       2007-03-01  6.27    1.71  5.30
## 568       2007-04-01  6.39    1.70  5.47
## 569       2007-05-01  6.39    1.64  5.47
## 570       2007-06-01  6.70    1.60  5.79
## 571       2007-07-01  6.65    1.65  5.73
## 572       2007-08-01  6.65    1.98  5.79
## 573       2007-09-01  6.59    2.07  5.74
## 574       2007-10-01  6.48    1.95  5.66
## 575       2007-11-01  6.40    2.25  5.44
## 576       2007-12-01  6.65    2.55  5.49
## 577       2008-01-01  6.54    2.80  5.33
## 578       2008-02-01  6.82    3.08  5.53
## 579       2008-03-01  6.89    3.38  5.51
## 580       2008-04-01  6.97    3.29  5.55
## 581       2008-05-01  6.93    3.05  5.57
## 582       2008-06-01  7.07    2.97  5.68
## 583       2008-07-01  7.16    3.15  5.67
## 584       2008-08-01  7.15    3.26  5.64
## 585       2008-09-01  7.31    3.62  5.65
## 586       2008-10-01  8.88    5.07  6.28
## 587       2008-11-01  9.21    5.68  6.12
## 588       2008-12-01  8.43    6.01  5.05
## 589       2009-01-01  8.14    5.62  5.05
## 590       2009-02-01  8.08    5.21  5.27
## 591       2009-03-01  8.42    5.60  5.50
## 592       2009-04-01  8.39    5.46  5.39
## 593       2009-05-01  8.06    4.77  5.54
## 594       2009-06-01  7.50    3.78  5.61
## 595       2009-07-01  7.09    3.53  5.41
## 596       2009-08-01  6.58    2.99  5.26
## 597       2009-09-01  6.31    2.91  5.13
## 598       2009-10-01  6.29    2.90  5.15
## 599       2009-11-01  6.32    2.92  5.19
## 600       2009-12-01  6.37    2.78  5.26
## 601       2010-01-01  6.25    2.52  5.26
## 602       2010-02-01  6.34    2.65  5.35
## 603       2010-03-01  6.27    2.54  5.27
## 604       2010-04-01  6.25    2.40  5.29
## 605       2010-05-01  6.05    2.63  4.96
## 606       2010-06-01  6.23    3.03  4.88
## 607       2010-07-01  6.01    3.00  4.72
## 608       2010-08-01  5.66    2.96  4.49
## 609       2010-09-01  5.66    3.01  4.53
## 610       2010-10-01  5.72    3.18  4.68
## 611       2010-11-01  5.92    3.16  4.87
## 612       2010-12-01  6.10    2.81  5.02
## 613       2011-01-01  6.09    2.70  5.04
## 614       2011-02-01  6.15    2.57  5.22
## 615       2011-03-01  6.03    2.62  5.13
## 616       2011-04-01  6.02    2.56  5.16
## 617       2011-05-01  5.78    2.61  4.96
## 618       2011-06-01  5.75    2.75  4.99
## 619       2011-07-01  5.76    2.76  4.93
## 620       2011-08-01  5.36    3.06  4.37
## 621       2011-09-01  5.27    3.29  4.09
## 622       2011-10-01  5.37    3.22  3.98
## 623       2011-11-01  5.14    3.13  3.87
## 624       2011-12-01  5.25    3.27  3.93
## 625       2012-01-01  5.23    3.26  3.85
## 626       2012-02-01  5.14    3.17  3.85
## 627       2012-03-01  5.23    3.06  3.99
## 628       2012-04-01  5.19    3.14  3.96
## 629       2012-05-01  5.07    3.27  3.80
## 630       2012-06-01  5.02    3.40  3.64
## 631       2012-07-01  4.87    3.34  3.40
## 632       2012-08-01  4.91    3.23  3.48
## 633       2012-09-01  4.84    3.12  3.49
## 634       2012-10-01  4.58    2.83  3.47
## 635       2012-11-01  4.51    2.86  3.50
## 636       2012-12-01  4.63    2.91  3.65
## 637       2013-01-01  4.73    2.82  3.80
## 638       2013-02-01  4.85    2.87  3.90
## 639       2013-03-01  4.85    2.89  3.93
## 640       2013-04-01  4.59    2.83  3.73
## 641       2013-05-01  4.73    2.80  3.89
## 642       2013-06-01  5.19    2.89  4.27
## 643       2013-07-01  5.32    2.74  4.34
## 644       2013-08-01  5.42    2.68  4.54
## 645       2013-09-01  5.47    2.66  4.64
## 646       2013-10-01  5.31    2.69  4.53
## 647       2013-11-01  5.38    2.66  4.63
## 648       2013-12-01  5.38    2.48  4.62
## 649       2014-01-01  5.19    2.33  4.49
## 650       2014-02-01  5.10    2.39  4.45
## 651       2014-03-01  5.06    2.34  4.38
## 652       2014-04-01  4.90    2.19  4.24
## 653       2014-05-01  4.76    2.20  4.16
## 654       2014-06-01  4.80    2.20  4.25
## 655       2014-07-01  4.73    2.19  4.16
## 656       2014-08-01  4.69    2.27  4.08
## 657       2014-09-01  4.80    2.27  4.11
## 658       2014-10-01  4.69    2.39  3.92
## 659       2014-11-01  4.79    2.46  3.92
## 660       2014-12-01  4.74    2.53  3.79
## 661       2015-01-01  4.45    2.57  3.46
## 662       2015-02-01  4.51    2.53  3.61
## 663       2015-03-01  4.54    2.50  3.64
## 664       2015-04-01  4.48    2.54  3.52
## 665       2015-05-01  4.89    2.69  3.98
## 666       2015-06-01  5.13    2.77  4.19
## 667       2015-07-01  5.20    2.88  4.15
## 668       2015-08-01  5.19    3.02  4.04
## 669       2015-09-01  5.34    3.17  4.07
## 670       2015-10-01  5.34    3.27  3.95
## 671       2015-11-01  5.46    3.20  4.06
## 672       2015-12-01  5.46    3.22  3.97
## 673       2016-01-01  5.45    3.36  4.00
## 674       2016-02-01  5.34    3.56  3.96
## 675       2016-03-01  5.13    3.24  3.82
## 676       2016-04-01  4.79    2.98  3.62
## 677       2016-05-01  4.68    2.87  3.65
## 678       2016-06-01  4.53    2.89  3.50
## 679       2016-07-01  4.22    2.72  3.28
## 680       2016-08-01  4.24    2.68  3.32
## 681       2016-09-01  4.31    2.68  3.41
## 682       2016-10-01  4.38    2.62  3.51
## 683       2016-11-01  4.71    2.57  3.86
## 684       2016-12-01  4.83    2.34  4.06
## 685       2017-01-01  4.66    2.23  3.92
## 686       2017-02-01  4.64    2.22  3.95
## 687       2017-03-01  4.68    2.20  4.01
## 688       2017-04-01  4.57    2.27  3.87
## 689       2017-05-01  4.55    2.25  3.85
## 690       2017-06-01  4.37    2.18  3.68
## 691       2017-07-01  4.39    2.07  3.70
## 692       2017-08-01  4.31    2.10  3.63
## 693       2017-09-01  4.30    2.10  3.63
## 694       2017-10-01  4.32    1.96  3.60
## 695       2017-11-01  4.27    1.92  3.57
## 696       2017-12-01  4.22    1.82  3.51
## 697       2018-01-01  4.26    1.68  3.55
## 698       2018-02-01  4.51    1.65  3.82
## 699       2018-03-01  4.64    1.80  3.87
## 700       2018-04-01  4.67    1.80  3.85
## 701       2018-05-01  4.83    1.85  4.00
## 702       2018-06-01  4.83    1.92  3.96
## 703       2018-07-01  4.79    1.90  3.87
## 704       2018-08-01  4.77    1.88  3.88
## 705       2018-09-01  4.88    1.88  3.98
## 706       2018-10-01  5.07    1.92  4.14
## 707       2018-11-01  5.22    2.10  4.22
## 708       2018-12-01  5.13    2.30  4.02
## 709       2019-01-01  5.12    2.41  3.93
## 710       2019-02-01  4.95    2.27  3.79
## 711       2019-03-01  4.84    2.27  3.77
## 712       2019-04-01  4.70    2.17  3.69
## 713       2019-05-01  4.63    2.23  3.67
## 714       2019-06-01  4.46    2.39  3.42
## 715       2019-07-01  4.28    2.22  3.29
## 716       2019-08-01  3.87    2.24  2.98
## 717       2019-09-01  3.91    2.21  3.03
## 718       2019-10-01  3.92    2.21  3.01
## 719       2019-11-01  3.94    2.13  3.06
## 720       2019-12-01  3.88    2.02  3.01
## 721       2020-01-01  3.77    2.01  2.94
## 722       2020-02-01  3.61    2.11  2.78
## 723       2020-03-01  4.29    3.42  3.02
## 724       2020-04-01  4.13    3.47  2.43
## 725       2020-05-01  3.95    3.28  2.50
## 726       2020-06-01  3.64    2.91  2.44
## 727       2020-07-01  3.31    2.69  2.14
## 728       2020-08-01  3.27    2.62  2.25
## 729       2020-09-01  3.36    2.68  2.31
## 730       2020-10-01  3.44    2.65  2.35
## 731       2020-11-01  3.30    2.43  2.30
## 732       2020-12-01  3.16    2.23  2.26
## 733       2021-01-01  3.24    2.16  2.45
## 734       2021-02-01  3.42    2.16  2.70
## 735       2021-03-01  3.74    2.13  3.04
## 736       2021-04-01  3.60    1.97  2.90
## 737       2021-05-01  3.62    2.00  2.96
## 738       2021-06-01  3.44    1.92  2.79
## 739       2021-07-01  3.24    1.92  2.57
## 740       2021-08-01  3.24    1.96  2.55
## 741       2021-09-01  3.23    1.86  2.53
## 742       2021-10-01  3.35    1.77  2.68
library(anytime)
bond$observation_date= anydate(bond$observation_date)
class(bond$observation_date)
## [1] "Date"
baa = bond[,2]
aaa = bond[,4]
spread = bond$BAA10YM
plot(baa ~ bond$observation_date, type = "l", main = "Baa Monthly Corporate Bond Yield")

plot(aaa~ bond$observation_date, type = "l", main = "AAA Monthly Corporate Bond Yield")

kpss test

kpss.test(baa, null = "Trend")
## Warning in kpss.test(baa, null = "Trend"): p-value smaller than printed p-value
## 
##  KPSS Test for Trend Stationarity
## 
## data:  baa
## KPSS Trend = 0.34773, Truncation lag parameter = 6, p-value = 0.01
kpss.test(aaa, null = "Trend")
## Warning in kpss.test(aaa, null = "Trend"): p-value smaller than printed p-value
## 
##  KPSS Test for Trend Stationarity
## 
## data:  aaa
## KPSS Trend = 0.42069, Truncation lag parameter = 6, p-value = 0.01
kpss.test(spread, null = "Trend")
## 
##  KPSS Test for Trend Stationarity
## 
## data:  spread
## KPSS Trend = 0.17893, Truncation lag parameter = 6, p-value = 0.0239

heteroskedasticity tests

library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
Box.test(arima_baa$residuals, type= "Ljung")
## 
##  Box-Ljung test
## 
## data:  arima_baa$residuals
## X-squared = 0.27531, df = 1, p-value = 0.5998
Box.test(arima_spread$residuals, type= "Ljung")
## 
##  Box-Ljung test
## 
## data:  arima_spread$residuals
## X-squared = 0.0033439, df = 1, p-value = 0.9539

Part 2: Fitting Model

train test split

train = bond[bond$observation_date >= "1974-01-01" & bond$observation_date <= "2020-10-01",][,1:2]
test = bond[bond$observation_date >= "2020-11-01" & bond$observation_date <= "2021-10-01",][,1:2]
train_aaa_temp = bond[bond$observation_date >= "1974-01-01" & bond$observation_date <= "2020-10-01",]
train_aaa = train_aaa_temp$AAA
test_aaa_temp = bond[bond$observation_date >= "2020-11-01" & bond$observation_date <= "2021-10-01",]
test_aaa = test_aaa_temp$AAA
train_baa_temp = bond[bond$observation_date >= "1974-01-01" & bond$observation_date <= "2020-10-01",]
train_baa = train_baa_temp$BAA
test_baa_temp = bond[bond$observation_date >= "2020-11-01" & bond$observation_date <= "2021-10-01",]
test_baa = test_baa_temp$BAA
baa_train1 <- bond[bond$observation_date >= "1974-01-01" & bond$observation_date <= "2016-10-01",]$BAA
baa_val1 <- bond[bond$observation_date >= "2016-11-01" & bond$observation_date <= "2017-10-01",]$BAA 
baa_train2 <- bond[bond$observation_date >= "1975-01-01" & bond$observation_date <= "2017-10-01",]$BAA 
baa_val2 <- bond[bond$observation_date >= "2017-11-01" & bond$observation_date <= "2018-10-01",]$BAA  
baa_train3 <- bond[bond$observation_date >= "1976-11-01" & bond$observation_date <= "2018-10-01",]$BAA 
baa_val3 <- bond[bond$observation_date >= "2018-11-01" & bond$observation_date <= "2019-10-01",]$BAA 
aaa_train1 <- bond[bond$observation_date >= "1974-01-01" & bond$observation_date <= "2016-10-01",]$AAA
aaa_val1 <- bond[bond$observation_date >= "2016-11-01" & bond$observation_date <= "2017-10-01",]$AAA 
aaa_train2 <- bond[bond$observation_date >= "1975-01-01" & bond$observation_date <= "2017-10-01",]$AAA 
aaa_val2 <- bond[bond$observation_date >= "2017-11-01" & bond$observation_date <= "2018-10-01",]$AAA  
aaa_train3 <- bond[bond$observation_date >= "1976-11-01" & bond$observation_date <= "2018-10-01",]$AAA 
aaa_val3 <- bond[bond$observation_date >= "2018-11-01" & bond$observation_date <= "2019-10-01",]$AAA 

Cointegration

Granger Causality Test

library(lmtest)
grangertest(aaa, baa, order = 2)
## Granger causality test
## 
## Model 1: baa ~ Lags(baa, 1:2) + Lags(aaa, 1:2)
## Model 2: baa ~ Lags(baa, 1:2)
##   Res.Df Df      F    Pr(>F)    
## 1    567                        
## 2    569 -2 9.6043 7.907e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

at 0.05 significance level, we reject the null hypothesis that aaa does not granger - cause baa; it is reasonable to use aaa yield to predict baa

grangertest(baa, aaa, order = 2)
## Granger causality test
## 
## Model 1: aaa ~ Lags(aaa, 1:2) + Lags(baa, 1:2)
## Model 2: aaa ~ Lags(aaa, 1:2)
##   Res.Df Df      F  Pr(>F)   
## 1    567                     
## 2    569 -2 5.1047 0.00635 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#install.packages("dynlm")
library(dynlm)
coint_model = dynlm(baa~aaa)
summary(coint_model)
## 
## Time series regression with "numeric" data:
## Start = 1, End = 574
## 
## Call:
## dynlm(formula = baa ~ aaa)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.56865 -0.28162 -0.07694  0.17943  2.43516 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.623325   0.046503    13.4   <2e-16 ***
## aaa         1.063667   0.005947   178.9   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4112 on 572 degrees of freedom
## Multiple R-squared:  0.9824, Adjusted R-squared:  0.9824 
## F-statistic: 3.199e+04 on 1 and 572 DF,  p-value: < 2.2e-16
res_coint_model = residuals(coint_model)
library(urca)
ur.df(res_coint_model)
## 
## ############################################################### 
## # Augmented Dickey-Fuller Test Unit Root / Cointegration Test # 
## ############################################################### 
## 
## The value of the test statistic is: -5.383

reject the null hypothesis - there is cointegration relationship

Predicting using cointegration model

ts_baa_train = ts(baa, start = c(1974,1), end = c(2020,9), frequency = 12)
ts_baa_test = ts(baa, start = c(2020,10), end = c(2021,10), frequency = 12)
ts_aaa_train = ts(aaa, start = c(1974,1), end = c(2020,9), frequency = 12)
ts_aaa_test = ts(aaa, start = c(2020,10), end = c(2021,11), frequency = 12)
data <- cbind(train_baa,train_aaa)
cajo <- ca.jo(data, ecdet = "none", type="eigen", K=2, spec="longrun")
summary(cajo)
## 
## ###################### 
## # Johansen-Procedure # 
## ###################### 
## 
## Test type: maximal eigenvalue statistic (lambda max) , with linear trend 
## 
## Eigenvalues (lambda):
## [1] 0.0431781498 0.0008714339
## 
## Values of teststatistic and critical values of test:
## 
##           test 10pct  5pct  1pct
## r <= 1 |  0.49  6.50  8.18 11.65
## r = 0  | 24.72 12.91 14.90 19.19
## 
## Eigenvectors, normalised to first column:
## (These are the cointegration relations)
## 
##              train_baa.l2 train_aaa.l2
## train_baa.l2     1.000000      1.00000
## train_aaa.l2    -1.071817      2.01103
## 
## Weights W:
## (This is the loading matrix)
## 
##             train_baa.l2  train_aaa.l2
## train_baa.d -0.051368166 -0.0006448633
## train_aaa.d  0.003724489 -0.0007780494

fitting a Vector Error Correction Model

library(Metrics)
## 
## Attaching package: 'Metrics'
## The following object is masked from 'package:forecast':
## 
##     accuracy
library(tsDyn)
## 
## Attaching package: 'tsDyn'
## The following object is masked from 'package:Metrics':
## 
##     mse
VECM_tsDyn = VECM(cbind(train_baa, train_aaa), lag=2, r=1,estim = "ML")
VECM_predict= VECM_tsDyn$residuals[,1]+ train_baa
## Warning in VECM_tsDyn$residuals[, 1] + train_baa: longer object length is not a
## multiple of shorter object length
matplot(cbind(VECM_predict, train_baa), type="l")
legend("topright",legend=c("Forecast train","Actual train"),
       col=c("black","red"), lty=1:5
       )

smape(VECM_predict, train_baa)
## [1] 0.01893392
plot(VECM_tsDyn$residuals[,1], type="l")

hist(VECM_tsDyn$residuals[,1])

library(Metrics)
predvecm<-predict(VECM_tsDyn, n.ahead=12)
smape(predvecm[,1], test_baa)
## [1] 0.06148065
Box.test(VECM_tsDyn$residuals[,1], type= "Ljung")
## 
##  Box-Ljung test
## 
## data:  VECM_tsDyn$residuals[, 1]
## X-squared = 0.0098522, df = 1, p-value = 0.9209

cross validation

VECM_val1 = VECM(cbind(baa_train1, aaa_train1), lag=2, r=1,estim = "ML")
VECM_predict_val1= VECM_val1$residuals[,1]+ baa_train1
## Warning in VECM_val1$residuals[, 1] + baa_train1: longer object length is not a
## multiple of shorter object length
smape(VECM_predict_val1, baa_train1)
## [1] 0.01808547
library(Metrics)
predvecm1<-predict(VECM_val1, n.ahead=12)
smape(predvecm1[,1], baa_val1)
## [1] 0.04835616
VECM_val2 = VECM(cbind(baa_train2, aaa_train2), lag=2, r=1,estim = "ML")
VECM_predict_val2= VECM_val2$residuals[,1]+ baa_train2
## Warning in VECM_val2$residuals[, 1] + baa_train2: longer object length is not a
## multiple of shorter object length
smape(VECM_predict_val2, baa_train2)
## [1] 0.01813966
predvecm2<-predict(VECM_val2, n.ahead=12)
smape(predvecm2[,1], baa_val2)
## [1] 0.09038143
VECM_val3 = VECM(cbind(baa_train3, aaa_train3), lag=2, r=1,estim = "ML")
VECM_predict_val3= VECM_val3$residuals[,1]+ baa_train3
## Warning in VECM_val3$residuals[, 1] + baa_train3: longer object length is not a
## multiple of shorter object length
smape(VECM_predict_val3, baa_train3)
## [1] 0.01859679
predvecm3<-predict(VECM_val3, n.ahead=12)
smape(predvecm3[,1], baa_val3)
## [1] 0.1018124

Prophet

#install.packages("prophet")
library(prophet)
## Loading required package: Rcpp
## Loading required package: rlang
## 
## Attaching package: 'rlang'
## The following object is masked from 'package:Metrics':
## 
##     ll
colnames(train) <- c("ds", "y")
m = prophet(train)
## Disabling weekly seasonality. Run prophet with weekly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
future <- make_future_dataframe(m, periods = 12)
forecast <- predict(m, future)
plot(m, forecast)

the fit doesn’t look good; try multivarate by adding AAA yield instead

multivariate

train$AAA= train_aaa
m=prophet(yearly.seasonality = T)
m <- add_regressor(m, 'AAA')
m <- fit.prophet(m, train)
## Disabling weekly seasonality. Run prophet with weekly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
future$AAA = bond[1:574,4]
forecast <- predict(m, future)

multivariate prophet evaluation - only add aaa

plot(m,forecast)

prophet_plot_components(m, forecast)

library(Metrics)
prophet_predicted = forecast$yhat
length(prophet_predicted)
## [1] 574
residuals_prophet = bond$BAA - prophet_predicted
plot(residuals_prophet, type="l")

hist(residuals_prophet)

smape(bond$BAA, prophet_predicted)
## [1] 0.0310414
smape(test_baa, tail(prophet_predicted,12))
## [1] 0.02539711
Box.test(residuals_prophet, type= "Ljung")
## 
##  Box-Ljung test
## 
## data:  residuals_prophet
## X-squared = 483.5, df = 1, p-value < 2.2e-16